/* ----------------------------------------------------------------------------------------------------------------
 * WorldData
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.engine.world 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Stage;
	import flash.events.EventDispatcher;
	import flash.geom.Rectangle;
	import flash.net.getClassByAlias;
	
	import pure.engine.entity.Entity;
	import pure.engine.entity.EntityManager;
	import pure.engine.entity.EntityPool;
	import pure.engine.entity.IEntity;
	import pure.engine.entity.IEntityFactory;
	import pure.engine.scenery.IScenery;
	import pure.engine.scenery.ImageScenery;
	import pure.engine.scenery.SceneryType;
	import pure.engine.scenery.TiledScenery;
	import pure.engine.system.SystemManager;
	import pure.engine.system.ns_despair;

	use namespace ns_despair;
	
	
	/// @eventType	pure.engine.events.EntityMouseEvent.CLICK
	[Event(name="click", type="pure.engine.events.EntityMouseEvent")] 

	/// @eventType	pure.engine.events.EntityMouseEvent.MOUSE_DOWN
	[Event(name="mouseDown", type="pure.engine.events.EntityMouseEvent")] 

	/// @eventType	pure.engine.events.EntityMouseEvent.MOUSE_UP
	[Event(name="mouseUp", type="pure.engine.events.EntityMouseEvent")] 

	/// @eventType	pure.engine.events.EntityMouseEvent.MOUSE_MOVE
	[Event(name = "mouseMove", type = "pure.engine.events.EntityMouseEvent")] 
	
	
	/**
	 * 世界Content
	 * 
	 * @author ＃Pure
	 * 
	 * @see WorldBody
	 */
public class WorldContent extends EventDispatcher
{
	
	
	//======================
	// Member
	//======================
	
	ns_despair var m_sceneryList:Array = [];

	ns_despair var m_entityList:Vector.<Entity> = new Vector.<Entity>;
			
	ns_despair var m_width:Number;
	
	ns_despair var m_height:Number;

	ns_despair var m_top:Number, m_bottom:Number, m_left:Number, m_right:Number;
	
	
	
	
	public function get stage():Stage    { return SystemManager.m_stage; }
	public function get top():Number     { return m_top; }
	public function get bottom():Number  { return m_bottom; }
	public function get left():Number    { return m_left; }
	public function get right():Number   { return m_right; }

	
	public function enter():void 
	{
	}

	
	public function exit():void
	{	
	}
	
	
	public function update(elapsedT:Number):void
	{
	}

	
	public function createEntity(name:String = 'default', layer:int = 0, active:Boolean = true):IEntity
	{
		var E:Entity;
		E = EntityPool.makeEntity(name,layer);
		E.m_visible = active;
		m_entityList.push(E);
		E.m_worldContent = this;
		
		return E;
	}
	
	
	public function createFinalEntity(alias:String, px:Number, py:Number,layer:int = 0, ...args):IEntity 
	{
		var classObject:Object = getClassByAlias(alias);
		var factory:IEntityFactory = new classObject() as IEntityFactory;
		var E:Entity = EntityPool.makeEntity(alias,layer);

		E.move(px, py);
		m_entityList.push(E);
		E.m_worldContent=this;
		
		return factory.produce(E, px, py, args);
	}
	
	
	public function createImageScenery(image:BitmapData, type:String = "steady"):IScenery
	{
		var scenery:IScenery = new ImageScenery(image, type);
		m_sceneryList.push(scenery);
		return scenery;
	}
	
	
	public function createTiledScenery(sheet:BitmapData, renderData:Array, tileSize:int, type:String = "steady"):IScenery
	{
		var scenery:IScenery = new TiledScenery(renderData, sheet, tileSize, type);
		m_sceneryList.push(scenery);
		return scenery;
	}
	

	
	
	/**
	 * 清除全部实体和布景
	 * 
	 * @see Monitor - ＃set worldData
	 */
	ns_despair function updateScope():void
	{
		
	}
	
	
	/**
	 * 清除全部实体和布景
	 * 
	 * @see WorldBody - ＃set worldData
	 */
	ns_despair function clearAll():void
	{
		var E:IEntity;
		var scenery:IScenery;
		var len:int;
		var i:int;
			
		len = m_entityList.length;
		for (i = len - 1; i >= 0; i-- )
		{
			E = m_entityList[i];
			E.kill();
			EntityPool.recycleEntity(E);
		}
		
		len = m_sceneryList.length;
		for (i = len - 1; i >= 0; i--) 
		{
			scenery = m_sceneryList[i];
			scenery.destroy();
		}
		
		m_sceneryList  =  null;
		m_entityList   =  null;
	}
	

	/**
	 * 获取布景缓存
	 * 
	 * @see Monitor - ＃_makeAllBuffers
	 */
	ns_despair function get sceneryBuffers():Array 
	{ 
		var buffers:Array = [];
		var scenery:IScenery;
		var len:int = m_sceneryList.length;
		for (var i:int = 0; i < len; i++) 
		{
			scenery = m_sceneryList[i];
			buffers.push(scenery.sceneryBuffer);
		}
		
		return buffers;
	}
	
	
	/**
	 * 更新区域布景
	 * 
	 * @param area
	 * @param containers
	 * 
	 * @see Monitor - ＃onFrame
	 */
	ns_despair function updateSceneriesByArea(area:Rectangle, containers:Array):void 
	{
		var scenery:IScenery;
		var len:int = m_sceneryList.length;
		var bitmap:Bitmap;
		var tx:Number;
		var ty:Number;
		
		for (var i:int = 0; i < len; i++)
		{
			scenery = m_sceneryList[i];
			bitmap = containers[i];
			
			switch(scenery.type)
			{
				// 完全流动
				case SceneryType.E_FLOW:
					tx = (scenery.width - area.width) * area.x / (area.width - m_width);
					ty = (scenery.height - area.height) * area.y / (area.height - m_height);
					break;
				
				// 水平流动
				case SceneryType.H_FLOW:
					tx = (scenery.width - area.width) * area.x / (area.width - m_width);
					ty = - area.y;
					break;
					
				// 垂直流动
				case SceneryType.V_FLOW:
					tx = - area.x;
					ty = (scenery.height - area.height) * area.y / (area.height - m_height);
					break;

				// 固定（Steady，Tile...）
				default:
					tx = - area.x;
					ty = - area.y;
					break;
			}
			
			bitmap.x = tx;
			bitmap.y = ty;
		}
	}



}

}